package com.ruoyi.advertisement.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.advertisement.domain.AdvertisementStrategy;
import com.ruoyi.advertisement.service.IAdvertisementStrategyService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 广告投放策略Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/strategy")
public class AdvertisementStrategyController extends BaseController
{
    @Autowired
    private IAdvertisementStrategyService advertisementStrategyService;

    /**
     * 查询广告投放策略列表
     */
    @RequiresPermissions("advertisement:strategy:list")
    @GetMapping("/list")
    public TableDataInfo list(AdvertisementStrategy advertisementStrategy)
    {
        startPage();
        List<AdvertisementStrategy> list = advertisementStrategyService.selectAdvertisementStrategyList(advertisementStrategy);
        return getDataTable(list);
    }

    /**
     * 导出广告投放策略列表
     */
    @RequiresPermissions("advertisement:strategy:export")
    @Log(title = "广告投放策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvertisementStrategy advertisementStrategy)
    {
        List<AdvertisementStrategy> list = advertisementStrategyService.selectAdvertisementStrategyList(advertisementStrategy);
        ExcelUtil<AdvertisementStrategy> util = new ExcelUtil<AdvertisementStrategy>(AdvertisementStrategy.class);
        util.exportExcel(response, list, "广告投放策略数据");
    }

    /**
     * 获取广告投放策略详细信息
     */
    @RequiresPermissions("advertisement:strategy:query")
    @GetMapping(value = "/{strategyId}")
    public AjaxResult getInfo(@PathVariable("strategyId") Long strategyId)
    {
        return success(advertisementStrategyService.selectAdvertisementStrategyByStrategyId(strategyId));
    }

    /**
     * 新增广告投放策略
     */
    @RequiresPermissions("advertisement:strategy:add")
    @Log(title = "广告投放策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvertisementStrategy advertisementStrategy)
    {
        return toAjax(advertisementStrategyService.insertAdvertisementStrategy(advertisementStrategy));
    }

    /**
     * 修改广告投放策略
     */
    @RequiresPermissions("advertisement:strategy:edit")
    @Log(title = "广告投放策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvertisementStrategy advertisementStrategy)
    {
        return toAjax(advertisementStrategyService.updateAdvertisementStrategy(advertisementStrategy));
    }

    /**
     * 删除广告投放策略
     */
    @RequiresPermissions("advertisement:strategy:remove")
    @Log(title = "广告投放策略", businessType = BusinessType.DELETE)
	@DeleteMapping("/{strategyIds}")
    public AjaxResult remove(@PathVariable Long[] strategyIds)
    {
        return toAjax(advertisementStrategyService.deleteAdvertisementStrategyByStrategyIds(strategyIds));
    }

    /**
     * 根据设备ID查询广告策略
     */
    @GetMapping("/device/{deviceId}")
    public AjaxResult getStrategyByDevice(@PathVariable("deviceId") String deviceId)
    {
        AdvertisementStrategy strategy = advertisementStrategyService.selectStrategyByDeviceId(deviceId);
        return success(strategy);
    }

    /**
     * 查询启用状态的策略列表
     */
    @RequiresPermissions("advertisement:strategy:query")
    @GetMapping("/enabled")
    public AjaxResult getEnabledStrategies()
    {
        List<AdvertisementStrategy> list = advertisementStrategyService.selectEnabledStrategies();
        return success(list);
    }

    /**
     * 启用/禁用策略
     */
    @RequiresPermissions("advertisement:strategy:edit")
    @Log(title = "广告策略状态变更", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestParam("strategyId") Long strategyId, 
                                   @RequestParam("status") Integer status)
    {
        return toAjax(advertisementStrategyService.changeStatus(strategyId, status));
    }

    /**
     * 复制策略
     */
    @RequiresPermissions("advertisement:strategy:add")
    @Log(title = "复制广告策略", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copyStrategy(@RequestParam("strategyId") Long strategyId, 
                                   @RequestParam("newStrategyName") String newStrategyName)
    {
        return toAjax(advertisementStrategyService.copyStrategy(strategyId, newStrategyName));
    }

    /**
     * 获取策略统计信息
     */
    @RequiresPermissions("advertisement:strategy:query")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        IAdvertisementStrategyService.StrategyStatistics statistics = 
            advertisementStrategyService.getStrategyStatistics();
        return success(statistics);
    }

    /**
     * 预览策略配置
     */
    @RequiresPermissions("advertisement:strategy:query")
    @GetMapping("/preview/{strategyId}")
    public AjaxResult previewStrategy(@PathVariable("strategyId") Long strategyId)
    {
        AdvertisementStrategy strategy = advertisementStrategyService.selectAdvertisementStrategyByStrategyId(strategyId);
        if (strategy == null) {
            return error("策略不存在");
        }
        
        // 返回策略详细信息，包含关联的设备和素材
        return success(strategy);
    }

    /**
     * 批量启用策略
     */
    @RequiresPermissions("advertisement:strategy:edit")
    @Log(title = "批量启用广告策略", businessType = BusinessType.UPDATE)
    @PutMapping("/batchEnable")
    public AjaxResult batchEnable(@RequestBody Long[] strategyIds)
    {
        int count = 0;
        for (Long strategyId : strategyIds) {
            count += advertisementStrategyService.changeStatus(strategyId, 1);
        }
        return count > 0 ? success("批量启用成功，共启用 " + count + " 条策略") 
                         : error("批量启用失败");
    }

    /**
     * 批量禁用策略
     */
    @RequiresPermissions("advertisement:strategy:edit")
    @Log(title = "批量禁用广告策略", businessType = BusinessType.UPDATE)
    @PutMapping("/batchDisable")
    public AjaxResult batchDisable(@RequestBody Long[] strategyIds)
    {
        int count = 0;
        for (Long strategyId : strategyIds) {
            count += advertisementStrategyService.changeStatus(strategyId, 0);
        }
        return count > 0 ? success("批量禁用成功，共禁用 " + count + " 条策略") 
                         : error("批量禁用失败");
    }
} 